require "SVNUtils.rb"
class DeployerStrategy

	def execute(projects,options)
		@projects = projects
		@options = options
		self.internal_execute()
	end
	
	def get_project(project_name)
	
		@projects.each do |p|
			if p.name == project_name then return p end
		end
	end
end
class ListProjectsStrategy < DeployerStrategy
	def internal_execute()
		counter = 1
		puts "Project list"
		@projects.each do |project|
			puts counter.to_s << ":\t" << project.name << " (" << project.root << ")"
		end
	end
end

class CommitProjectStrategy < DeployerStrategy
	def internal_execute()
		if @options[:projectname].nil? then
			puts "No project name defined, must define for commit."
			puts "Example: deployer -p PROJECT_NAME"
			return
		end
		utils = SVNUtils.new()
		utils.verbose = @options[:verbose]
		project = self.get_project(@options[:projectname])
		puts "Commiting project " + project.name 
		if(@options[:verbose]) then puts project.root end
		
		
		revision_number = utils.get_repository_revision(project.root)
		tag_name = "" 
		if not @options[:tagname].nil? then
			tag_name = @options[:tagname]
		else
			tag_name = project.acronym + "_" + project.major_version.to_s + "." + project.minor_version.to_s + "." + revision_number 
		end
		
		commit_message =  "Commit made by Deployer"
		
		if(@options[:verbose]) then puts "Creating tag #{tag_name}" end
		utils.svn_copy(project.trunk_dir, project.tag_dir+tag_name,commit_message)
		
		if(@options[:commitlatest]) then
			
			puts "Removing tag #{project.latest_tag}"
			utils.svn_remove_tag(project.tag_dir,project.latest_tag)
			puts "Creating tag #{project.latest_tag}"
			utils.svn_copy(project.trunk_dir, project.tag_dir+project.latest_tag+"/",commit_message)
		end
	end
end